package demo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * socket编程client
 * 1)Socket socket = new Socker(host,port)
 * 2)socket.getOutputStream()写内容
 * 3)socket.getInputStream()读内容
 *
 * DataInputStream和DataOutputStream read／writeUTF指定编码
 */
public class F_19 {
	public static void main(String[] args) {
		String serverName = "127.0.0.1";
		int port = Integer.parseInt("8888");
		DataOutputStream out = null;
		DataInputStream in = null;
		Socket client = null;
		try
		{
			client = new Socket(serverName, port);
			OutputStream outToServer = client.getOutputStream();
			out = new DataOutputStream(outToServer);
			//TODO 发送的内容
			out.writeUTF("19");
			out.flush();
			InputStream inFromServer = client.getInputStream();
			in = new DataInputStream(inFromServer);
			System.out.println(in.readUTF());

		}catch(IOException e){
			e.printStackTrace();
		}finally {
			try {
				out.close();
				in.close();
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}


		}
	}


}
